<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="replay">播放录像</button>
</body>
</html>
<script>
/** 宏命令 */ 
let closeDoorCommand = {
  execute() {
    console.log('关门');
  }
}
let openPcCommand = {
  execute() {
    console.log('开电脑');
  }
}
let openQQCommand = {
  execute() {
    console.log('登录QQ');
  }
}
class MacroCommand {
  constructor() {
    this.commandsList = []
  }
  add(command) {
    this.commandsList.push(command)
  }
  execute() {
    this.commandsList.map(command => command.execute())
  }
}
let macroCommand = new MacroCommand()
macroCommand.add(closeDoorCommand)
macroCommand.add(openPcCommand)
macroCommand.add(openQQCommand)

macroCommand.execute()

/** 撤销和重做 */
let Ryu = {
  attack() {
    console.log('攻击')
  },
  defense() {
    console.log('防御')
  },
  jump() {
    console.log('跳跃')
  },
  crouch() {
    console.log('蹲下')
  }
}
function makeCommand(receiver, state) {
  return () => {
    receiver[state]()
  }
}
let commands = {
  "119": "jump", // W
  "115": "crouch", // S
  "97": "defense", // A
  "100": "attack" // D
}

let commandStack = [] // 保存命令的堆栈
document.onkeypress = ev => {
  let keyCode = ev.keyCode,
    command = makeCommand(Ryu, commands[keyCode])
  if(command) {
    command() // 执行命令
    commandStack.push(command) // 记录执行过的命令
  }
}
document.getElementById('replay').onclick = () => {
  let command
  while(command = commandStack.shift()) {
    command()
  }
}
</script>